package service

import (
	"astgram/common"
	"astgram/dal/po"
	"astgram/dal/repo"
	"astgram/model"
	"astgram/model/vo"
	"github.com/gin-gonic/gin"
	"log"
)

type GetImagesService struct {
}

func (ss GetImagesService) Execute(c *gin.Context, req *model.GetImagesRequest) (*model.GetImagesResponse, error) {
	if req.Limit == 0 {
		req.Limit = 40
	}
	log.Printf("GetImagesService.Execute, c is %+v", c)
	imageRepo := repo.NewImageRepo()
	pos, total, err := imageRepo.GetImagesPos(req.Limit, req.Offset, req.ImageID)
	if err != nil {
		return nil, err
	}
	vos := ToImageVos(pos)
	// load comments, users
	return &model.GetImagesResponse{
		Meta:   common.MetaOk,
		Images: vos,
		Total:  total,
	}, nil
}

func ToImageVos(pos []*po.Image) []*vo.Image {
	vos := make([]*vo.Image, len(pos))
	for i := range pos {
		vos[i] = pos[i].ToVo()
	}
	return vos
}
